use std::{
    fs::File,
    io::BufReader,
    sync::{Arc, RwLock},
};

use lazy_static::lazy_static;
use rodio::{Decoder, OutputStream, Sink};

// TODO: what the fuck is lazystatuc
lazy_static! {
    pub static ref RING: Arc<RwLock<Sink>> = init_local_ring();
}

const RING_PATH: &'static str = "data/ring.mp3";

fn init_local_ring() -> Arc<RwLock<Sink>> {
    let mut _path = std::env::current_dir().unwrap();
    _path.push(RING_PATH);

    let (_stream, stream_handler) = OutputStream::try_default().unwrap();
    let file = BufReader::new(File::open(_path).unwrap());

    let sink = Sink::try_new(&stream_handler).unwrap();
    let source = Decoder::new(file).unwrap();

    sink.append(source);

    Arc::new(RwLock::new(sink))
}

pub fn get_local_ring() -> Sink {
    let mut _path = std::env::current_dir().unwrap();
    _path.push(RING_PATH);

    let (_stream, stream_handler) = OutputStream::try_default().unwrap();
    let file = BufReader::new(File::open(_path).unwrap());

    let sink = Sink::try_new(&stream_handler).unwrap();
    let source = Decoder::new(file).unwrap();
    sink.append(source);
    sink
}

pub fn local_ring() {
    let mut _path = std::env::current_dir().unwrap();
    _path.push(RING_PATH);

    let (_stream, stream_handler) = OutputStream::try_default().unwrap();

    let file = BufReader::new(File::open(_path).unwrap());

    let sink = Sink::try_new(&stream_handler).unwrap();

    let source = Decoder::new(file).unwrap();
    sink.append(source);
    sink.play();
    std::thread::sleep(std::time::Duration::from_secs(10)); // why sleep
}
